package cn.tradewin.advisor.pages.project;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.PersistenceConstants;
import org.apache.tapestry5.SelectModel;
import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.annotations.Persist;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.beaneditor.BeanModel;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.services.BeanModelSource;
import org.slf4j.Logger;

import cn.tradewin.advisor.commons.selectmodel.CodeLabelSelectModel;
import cn.tradewin.advisor.exceptions.DBException;
import cn.tradewin.advisor.persist.model.Project;
import cn.tradewin.advisor.persist.services.DeptService;
import cn.tradewin.advisor.persist.services.ProjectService;

public class ProjectBoard {
	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private Byte dept;
	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private Date start;
	
	@Property
	@Persist(PersistenceConstants.FLASH)
	private Date end;
	
	@Inject
	private Messages messages;
	
	@Inject
	private BeanModelSource beanModelSource;
	
	@Property
	private BeanModel<Project> beanModel;
	
	@Inject
	private ProjectService projectService;
	
	@Property
	private List<Project> projects;
	
	@Property
	private Project project;
	
	@Property
	private SelectModel deptModel;

	@Inject
	private DeptService deptService;

	@Inject
	private Logger log;
	
	@SetupRender
	void setupRender() throws DBException {
		beanModel = beanModelSource.createDisplayModel(Project.class, messages);
		beanModel.add("action", null);
		beanModel.include("name", "contractId", "start", "end", "deptName","status", "action");
		beanModel.get("name").sortable(false);
		beanModel.get("contractId").sortable(true);
		beanModel.get("start").sortable(false);
		beanModel.get("end").sortable(false);
		beanModel.get("deptName").sortable(false);
		beanModel.get("status").sortable(false);
		deptModel = new CodeLabelSelectModel(deptService.getAllDeptsOptions());
		projects = projectService.listAllProjects(dept, start, end);
		log.info("***********************************");
	}	
	
//	@OnEvent(EventConstants.PREPARE_FOR_RENDER)
//	void prepareForRender() throws DBException {
//		deptModel = new CodeLabelSelectModel(deptService.getAllDeptsOptions());
//		projects = projectService.listAllProjects();
//	}
	
	@Log
	void onSubmit() {
		log.info("dept = " + dept);
		log.info("start = " + start);
//		projects = projectService.listAllProjects(dept, start, end);
	}
}
